<?php

/**
 * Description of model_users
 *
 * @author Steffen Bilde
 *
 *
 */

class Model_users extends CI_Model
{
    function  __construct()
    {
        parent::__construct();
    }

    // Function that list all users in alphabetical order.
    function listAllUsers()
    {
        $data = array();
        $this->db->order_by('last_name','asc');
        $Q = $this->db->get('user');

        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }


    // Function that gets all usernames.
    function getAllUsernames()
    {
        $data = array();
	$this->db->select("id,username");
	$this->db->order_by('username','asc');
	$Q = $this->db->get("user");

        if ($Q->num_rows() > 0)
        {
           foreach ($Q->result() as $row)
           {
               $data[$row->id] = $row->username;
           }
        }
        $Q->free_result();
        return $data;
    }



    // Function that gets a user by id:
    function getUser($id)
    {
        $data = array();
        $options = array('id' => $id);
        $Q = $this->db->getwhere('user',$options,1);

        if ($Q->num_row() > 0)
        {
            $data = $Q->row_array();
        }

        $Q->free_result();
        return $data;

    }
    
    //GET A USER BY USERNAME //change to 'id'
    function getUserByUsername($id)
    {
        $data = array();
        $this->db->where('id',$id);
        $this->db->limit(1);
        $Q = $this->db->get('user');
        if ($Q->num_rows() > 0)
        {         
            $data = $Q->row_array();           
        }

        $Q->free_result();
        return $data;
    }

    //Function that gets limited user-info of user.
    function getUserLimited($id)
    {
        $data = array();
	$this->db->select('id,username,first_name,last_name,email,phone');
	$this->db->where('id',$id);
	$this->db->limit(1);
	$Q = $this->db->get('user');

	if ($Q->num_rows() > 0)
        {
            $data = $Q->row();
        }

        $Q->free_result();
        return $data;
    }

    // Function that validates username and password when a user logs in.
    function validate()
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $Q = $this->db->get('user');

        if($Q->num_rows == 1)
        {
            $row = $Q->row_array();
            $data = array(
                'id' => $row['id'],
                'username' => 'username',
                'createdate' => $row['created_date'],
                'firstname' => $row['first_name'],
                'lastname' => $row['last_name'],
                'birthday' => $row['birthday'],
                'email' => $row['email'],
                'phone' => $row['phone'],
                'bio' => $row['bio'],
                'status' => $row['status'],
                'rankid' => $row['rank_id'],
                'wallid' => $row['wall_id'],
                'numbpost' => $row['numb_post'],
                'is_logged_in' => true,
            );
            $this->session->set_userdata($data);
            return true;
        }
    }

    // function that creates a an array with userinformation and inserts it to the database.
    function create_user()
    {
        $new_user_insert_data = array(
                'first_name' => $this->input->post('first_name'),
                'last_name' => $this->input->post('last_name'),
                'email' => $this->input->post('email'),
                'username' => $this->input->post('username'),
                'password' => md5($this->input->post('password'))

        );
        $insert = $this->db->insert('user', $new_user_insert_data);
        return $insert;
    }


    // funciton that gets an image form the database, and makes it ready to view on a site.
    function get_profile_photo($id)
    {
        $this->db->select('photo');
        $this->db->where('id',$id);
        $this->db->limit(1);
        $Q = $this->db->get('user');
        if ($Q->num_rows() > 0){
                $data = $Q->row();
        }
        //echo $this->db->last_query();
        if (strlen($data->photo))
        {
            echo base64_decode($data->photo);             
        }
        else
        {
            echo img('images/fpo.gif');
        }
    }


    function update_user($id) {
        $data = array(
            'first_name' => xss_clean(substr($this->input->post('firstname'),0,255)),
            'last_name' => xss_clean(substr($this->input->post('lastname'),0,255)),
            'phone' => xss_clean(substr($this->input->post('phone'), 0, 16)),
            'bio' => xss_clean(substr($this->input->post('bio'), 0,5000))
        );

        if (strlen($this->input->post('password'))) {
            $data['password'] = md5($this->input->post('password'));
        }

        if ($_FILES['photo']['size'] > 0) {
            $fname = $_FILES['photo']['name'];
            $fsize = $_FILES['photo']['size'];
            $ftype = $_FILES['photo']['type'];
            $ftemp = $_FILES['photo']['tmp_name'];

            $fp = fopen($ftemp, 'r');
            $content = fread($fp, filesize($ftemp));
            fclose($fp);
            // Encodes the pictures with base64 this means,
            // that we have to decode it again when we get it form the db.
            $data['photo'] = base64_encode($content);
        }

        $this->db->where('id', $id);
        $this->db->update('user', $data);

        return $id;

    }

    //SEARCH USERS
    function search_users($input,$users_from_tags){
            $data = array();
            $term = xss_clean(substr($input,0,255));
            $this->db->select('id,username,first_name,last_name,email,phone');
            $this->db->like('first_name', $term);
            $this->db->or_like('last_name', $term);
            $this->db->or_like('email', $term);
            $this->db->or_like('phone', $term);
            $this->db->or_like('bio', $term);
            $this->db->where('status','active');
            $Q = $this->db->get("user");
            if ($Q->num_rows() > 0){
                    foreach ($Q->result() as $row){
                            $ID = $row->id;
                            $data[$ID] = $row;
                    }
            }

            if (count($users_from_tags)){
                    foreach ($users_from_tags as $mid => $truth){
                            if (!isset($data[$mid])){
                                    $data[$mid] = $this->get_user_short($mid);
                            }
                    }
            }


            $Q->free_result();
            return $data;
    }
    

   
}
?>